-- Get variables from setting
local rewardPerTenHP = get ("mRewardPerTenHP")
local friendlyFire = get ("mFriendlyFire")

-- Function that starts when a player gets hit
function medic (attacker, weapon, bodypart, loss)
	if (attacker) then
		if getElementType (attacker) == "player" then
			local attackerTeam = getPlayerTeam (attacker)
			if (attackerTeam) then
				teamName = getTeamName (attackerTeam)
			end
			local currentHealth = getElementHealth(source)
			-- Check if attacker is medic and uses a flower
			if (teamName == "medics") and (weapon == 14)  then
				local addHealth = loss + 10
				-- Make sure no health is lost and add 10HP
				setElementHealth (source, currentHealth + addHealth)
				-- Check if person has less then 100HP
				if (getElementHealth (source) < 100) then
					-- Give the medic a reward
					givePlayerMoney (attacker, rewardPerTenHP)
				else
					outputChatBox ("Person already has 100% health!", attacker)
				end
			end
		end
	end
end

-- Make a player a medic when he inserts /medic or go to medic marker
function becomeMedic ()
	local teamMedics = getTeamFromName ("medics")
	local playerTeam = getPlayerTeam (source)
	if (playerTeam) ~= (teamMedics) then
		setPlayerTeam (source, teamMedics)
		setElementModel (source, 274)
		giveWeapon (source, 22, 1700)
		giveWeapon (source, 14, 1, true)
		setPedAnimation (source, "CLOTHES", "CLO_Buy", -1, false)
		setTimer (setPedAnimation, 2500, 1, source, false)
		outputChatBox ("You are now a medic!", source)
	else
		outputChatBox ("You already are a medic.", source)
	end
end

addEvent("acceptMedic", true)
addEventHandler("acceptMedic", getRootElement(), becomeMedic)
addEventHandler ("onPlayerDamage", getRootElement(), medic)